package com.scuec.trainingmanagementsys.controller;

import com.scuec.trainingmanagementsys.entity.Grade;
import com.scuec.trainingmanagementsys.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/grade")
@CrossOrigin
public class GradeController {
    @Autowired
    private GradeService gradeService;

    @PostMapping("/save")
    public Map<String, Object> saveGrade(@RequestBody Grade grade) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = gradeService.saveGrade(grade);
            result.put("success", rows > 0);
            result.put("message", rows > 0 ? "评分保存成功" : "评分保存失败");
            result.put("data", grade);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "评分保存失败");
            e.printStackTrace();
        }
        return result;
    }

    @PutMapping("/update")
    public Map<String, Object> updateGrade(@RequestBody Grade grade) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = gradeService.updateGrade(grade);
            result.put("success", rows > 0);
            result.put("message", rows > 0 ? "评分更新成功" : "评分更新失败");
            result.put("data", grade);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "评分更新失败");
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/project/{projectId}/team/{teamId}")
    public Map<String, Object> getGradeByProjectAndTeam(@PathVariable Integer projectId, @PathVariable Integer teamId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Grade grade = gradeService.getGradeByProjectAndTeam(projectId, teamId);
            if (grade != null) {
                result.put("code", 200);
                result.put("data", grade);
                result.put("message", "success");
            } else {
                result.put("code", 404);
                result.put("message", "未找到评分记录");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取评分失败");
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/project/{projectId}")
    public Map<String, Object> getGradesByProjectId(@PathVariable Integer projectId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Grade> grades = gradeService.getGradesByProjectId(projectId);
            result.put("code", 200);
            result.put("data", grades);
            result.put("message", "success");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取评分失败");
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/project/{projectId}/student/{studentId}")
    public Map<String, Object> getGradeByProjectAndStudent(@PathVariable Integer projectId, @PathVariable Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Grade grade = gradeService.getGradeByProjectAndStudent(projectId, studentId);
            result.put("code", 200);
            result.put("data", grade);
            result.put("message", "success");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取评分失败");
            e.printStackTrace();
        }
        return result;
    }
    // 新增：通过学生ID和项目ID获取评分（学生端使用）
    @GetMapping("/student/{studentId}/project/{projectId}")
    public Map<String, Object> getGradeByStudentAndProject(
            @PathVariable Integer studentId,
            @PathVariable Integer projectId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Grade grade = gradeService.getGradeByProjectAndStudent(projectId, studentId);
            if (grade != null) {
                result.put("code", 200);
                result.put("data", grade);
                result.put("message", "success");
            } else {
                result.put("code", 404);
                result.put("message", "未找到该学生的成绩记录");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取评分失败");
            e.printStackTrace();
        }
        return result;
    }
}